/**
 * Created by jin on 2015/8/24.
 */
var cityArray = new Array();
var hospitalArray = new Array();
$(document).ready(function(){
    $('#province').change(function () {
        var proId = $(this).val();
        var cityList = null;
        if(typeof cityArray[proId] == 'undefined')
        {
            $.ajax({
                url:getCityListUrl,
                type:'POST',
                data:{'pro_id':proId},
                async:false,
                success: function (data) {
                    cityArray[proId] = data;
                }
            });
        }
        $.ajax({
                url:getHosProListUrl,
                type:'POST',
                data:{'pro_id':proId},
                async:false,
                success: function (data) {
                    hospitalArray[proId] = data;
                }
            });
        hospitalList = hospitalArray[proId];
        hospitalAddOption(hospitalList);
        cityList = cityArray[proId];
        cityAddOption(cityList);
    });
    $('#city').change(function () {
        var cityId = $(this).val();
        var hospitalList = null;
        $.ajax({
            url:getHosListUrl,
            type:'POST',
            data:{'city_id':cityId},
            async:false,
            success: function (data) {
                hospitalArray[cityId] = data;
            }
        });
        hospitalList = hospitalArray[cityId];
        hospitalAddOption(hospitalList);
    });
    $('#addHospitalButton').click(function () {
       try
       {
           //检测表单是否填写正确
           var checkInputArray = new Array('input[name="hos_name"]','input[name="hos_tel"]','input[name="hos_detailed_address"]','select[name="province"]','select[name="city"]','select[name="level"]','input[name="hos_image"]');
           for(var i = 0;i<checkInputArray.length;i++)
           {
               if($(checkInputArray[i]).val().length <= 0)
               {
                   throw new Error(checkInputArray[i],"asdasdasd");
               }
           }
           var checkTextarea = new Array('textarea[name="hos_route"]','textarea[name="hos_description"]');
           for(var i = 0;i<checkTextarea.length;i++)
           {
               if($(checkTextarea[i]).val().length <= 0)
               {
                   throw new Error(checkTextarea[i],"asdasdasd");
               }
           }
           return true;
       }
       catch(e)
       {
           $(e.message).focus();
           return false;
       }
    });
    function cityAddOption(cityList){
        var dataLength = cityList.length;
        var citySelect = $('#city');
        citySelect.html('');
        for(var i = 0;i<dataLength;i++)
        {
            citySelect.append('<option value="'+cityList[i]['city_id']+'">'+cityList[i]['city_name']+'</option>');
        }
    }
    function hospitalAddOption(hospitalList){
        var dataLength = hospitalList.length;
        var hospitalSelect = $('#hospital');
        hospitalSelect.html('');
        for(var i = 0;i<dataLength;i++)
        {
            hospitalSelect.append('<option value="'+hospitalList[i]['hos_id']+'">'+hospitalList[i]['hos_name']+'</option>');
        }
    }
});